package com.blue.base.common.bean.resp;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.blue.base.common.enums.ServiceEnum;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author liulei
 * @version 1.0
 */
public class JsonResult<T> implements Serializable {

    public JsonResult() {

    }

    private static final long serialVersionUID = 1752571016357023678L;

    private int status;
    private String message;
    private int service;
    private T data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getService() {
        return service;
    }

    public void setService(int service) {
        this.service = service;
    }

    public static JsonResult ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> result = new JsonResult();
        result.setStatus(HttpStatus.HTTP_OK);
        result.setMessage("OK");
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public boolean isOk() {
        return this.status == HttpStatus.HTTP_OK;
    }

    public static <T> JsonResult<T> failed(String message) {
        JsonResult result = new JsonResult();
        result.setStatus(500);
        result.setMessage(message);
        return result;
    }

    public static <T> JsonResult<T> failed(ServiceEnum service, String message) {
        JsonResult<T> result = new JsonResult();
        result.setService(service.getCode());
        result.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
        result.setMessage(message);
        return result;
    }

    public static <T> JsonResult<T> define(ServiceEnum service, Integer code, String message) {
        JsonResult<T> result = new JsonResult();
        result.setService(service.getCode());
        result.setStatus(code);
        result.setMessage(message);
        return result;
    }

    public static <T> JsonResult<T> define(Integer code, String message) {
        JsonResult<T> result = new JsonResult();
        result.setStatus(code);
        result.setMessage(message);
        return result;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof JsonResult)) {
            return false;
        }
        JsonResult<T> result = (JsonResult) o;
        return status == result.status &&
                Objects.equals(message, result.message) &&
                Objects.equals(data, result.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(status, message, data);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

